<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="order")
 */
class Order extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\Order();

    }


    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $userId = input('user_id', 0, 'intval');
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where, $extra) = $this->buildTableParames(['shop_name']);
            if (!empty($extra['shop_name'])) {
                $where[] = ['s.name', 'like', '%' . $extra['shop_name'] . '%'];
            }
            if ($userId) {
                $where[] = ['o.user_id', '=', $userId];
            }
            //var_dump($where);
            if ($this->user['role'] == 1) {
                $shopIds = \app\common\model\Shop::where('user_id', $this->user['id'])->column('id');
                if ($shopIds) {
                    $where[] = ['o.shop_id', 'in', $shopIds];
                }
            }
            $count = $this->model
                ->alias('o')
                ->leftJoin('shop s', 's.id=o.shop_id')
                ->leftJoin('user u', 'u.id=o.user_id')
                ->field('o.*,s.name as shop_name,u.nickname')
                ->where($where)
                ->count();
            $list = $this->model
                ->alias('o')
                ->leftJoin('shop s', 's.id=o.shop_id')
                ->leftJoin('user u', 'u.id=o.user_id')
                ->leftJoin('goods g', 'g.id=o.goods_id')
                ->leftJoin('goods_sku gs', 'gs.id=o.sku_id')
                ->field('o.*,s.name as shop_name,u.nickname,g.name as goods_name,gs.sku_json')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            foreach ($list as $k => $item) {
                $list[$k]['express_time'] = $item['express_time'] ? date('Y-m-d H:i:s', $item['express_time']) : '-';
                $arr = json_decode($item['sku_json'],true);
                $str = '';
                foreach ($arr as $f => $v) {
                    if($v['name'] != '默认'){
                        $str .= $v['name'].':'.$v['value'].'; ';
                    }
                }
                $list[$k]['goods_name'] .= ' '.$str;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }


        $this->assign('user_id', $userId);

        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="发货")
     */
    public function send()
    {
        $id = input('id', 0, 'intval');
        try {
            $order = \app\common\model\Order::find($id);
            if ($order->express_time == 0) {
                $order->express_time = time();
                $order->save();
            }
        } catch (\Exception $exception) {
            return $this->error('发货失败');
        }
        return $this->success('发货成功');
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index2()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where, $extra) = $this->buildTableParames(['shop_name', 'express_status']);
            if (!empty($extra['shop_name'])) {
                $where[] = ['s.name', 'like', '%' . $extra['shop_name'] . '%'];
            }
            $where[] = ['o.goods_shop_id', '=', 0];

            if (!empty($extra['express_status'])) {
                $express_status = $extra['express_status'];
                if ($express_status == 1) { //已发货
                    $where[] = ['o.express_time', '<>', 0];
                } elseif ($express_status == 2) { //待发货
                    $where[] = ['o.express_time', '=', 0];
                    $where[] = ['o.pay_status', '=', 1];
                    $where[] = ['o.refund_status', '=', 0];
                }
            }
            $count = $this->model
                ->alias('o')
                ->leftJoin('shop s', 's.id=o.shop_id')
                ->leftJoin('user u', 'u.id=o.user_id')
                ->leftJoin('goods g', 'o.goods_id=g.id')
                ->leftJoin('goods_sku gs', 'o.sku_id=gs.id')
                ->field('o.*')
                ->where($where)
                ->count();
            $list = $this->model
                ->alias('o')
                ->leftJoin('shop s', 's.id=o.shop_id')
                ->leftJoin('user u', 'u.id=o.user_id')
                ->leftJoin('goods g', 'o.goods_id=g.id')
                ->leftJoin('goods_sku gs', 'o.sku_id=gs.id')
                ->field('o.*,s.name as shop_name,s.address as shop_address,u.nickname,g.name as goods_name,g.img as goods_img,gs.img as sku_img,gs.sku_json')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k => $item) {
                //如果未支付 显示  无需发货
                if ($item['pay_status'] != 1 || $item['refund_status'] != 0) {
                    $list[$k]['express_status'] = 3;
                } else {
                    $list[$k]['express_status'] = $item['express_time'] ? 1 : 2;
                }
                $list[$k]['express_time'] = $item['express_time'] ? date('Y-m-d H:i:s', $item['express_time']) : '-';
                //处理sku_json
                if ($item['sku_json']) {
                    $arr = json_decode($item['sku_json'], true);
                    $list[$k]['sku_json'] = $arr[0]['name'] . '-' . $arr[0]['value'];
                }
                if ($item['sku_img']) {
                    $list[$k]['goods_img'] = $item['sku_img'];
                }
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

}